 using Microsoft.EntityFrameworkCore;
using TeachingManagementSystem.Models;
using System;

namespace TeachingManagementSystem.DAL
{
    public class DataContext : DbContext
    {
        // 数据库表映射
        public DbSet<User> Users { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Classroom> Classrooms { get; set; }
        public DbSet<Schedule> Schedules { get; set; }

        // 数据库连接字符串
        private const string ConnectionString = "server=localhost;port=3306;database=TeachingManagementSystem;user=root;password=;charset=utf8mb4;";

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // 配置使用MySQL数据库并添加错误处理
            optionsBuilder.UseMySQL(ConnectionString)
                .LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Error);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置实体关系和约束
            modelBuilder.Entity<Course>()
                .HasOne<User>()
                .WithMany()
                .HasForeignKey(c => c.TeacherID)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Schedule>()
                .HasOne<Course>()
                .WithMany()
                .HasForeignKey(s => s.CourseID)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<Schedule>()
                .HasOne<Classroom>()
                .WithMany()
                .HasForeignKey(s => s.ClassroomID)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Schedule>()
                .HasOne<User>()
                .WithMany()
                .HasForeignKey(s => s.TeacherID)
                .OnDelete(DeleteBehavior.Restrict);

            // 配置DayOfWeek属性映射为整数
            modelBuilder.Entity<Schedule>()
                .Property(s => s.DayOfWeek)
                .HasConversion(
                    v => (int)v,
                    v => (DayOfWeek)v);

            // 配置UserRole属性映射为字符串
            modelBuilder.Entity<User>()
                .Property(u => u.Role)
                .HasConversion(
                    v => v.ToString(),
                    v => (UserRole)Enum.Parse(typeof(UserRole), v));

            // 配置CourseStatus属性映射为字符串
            modelBuilder.Entity<Course>()
                .Property(c => c.Status)
                .HasConversion(
                    v => v.ToString(),
                    v => (CourseStatus)Enum.Parse(typeof(CourseStatus), v));

            // 配置ClassroomType枚举转换器
            modelBuilder.Entity<Classroom>()
                .Property(c => c.Type)
                .HasConversion(
                    v => v.ToString(),
                    v => string.Equals(v, "Laboratory", StringComparison.OrdinalIgnoreCase) ? ClassroomType.Laboratory : 
                         string.Equals(v, "Multimedia", StringComparison.OrdinalIgnoreCase) ? ClassroomType.Multimedia : 
                         string.Equals(v, "Auditorium", StringComparison.OrdinalIgnoreCase) ? ClassroomType.Auditorium : 
                         string.Equals(v, "MeetingRoom", StringComparison.OrdinalIgnoreCase) ? ClassroomType.MeetingRoom : 
                         ClassroomType.Regular
                );

            // 配置唯一性约束
            modelBuilder.Entity<User>()
                .HasIndex(u => u.Username)
                .IsUnique();

            modelBuilder.Entity<Course>()
                .HasIndex(c => c.CourseCode)
                .IsUnique();

            modelBuilder.Entity<Classroom>()
                .HasIndex(c => c.ClassroomNumber)
                .IsUnique();
        }
    }
}